<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Sign In
 * @description : User sign up mobile
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Signup extends MOBILE_Controller {

  public function index() {

    // Checking request type
    if ($this->is_post()) {

      // Getting inputs
      $inno_email = strtolower($this->input->post('email', TRUE));
      $user_password = 'innovature' . rand(111, 999);

      // Checking inputs
      if (!empty($inno_email) && !empty($user_password)) {

        if (filter_var($inno_email, FILTER_VALIDATE_EMAIL)) {

          $email_data = explode('@', $inno_email);

          if (!empty($email_data) && is_array($email_data) && end(($email_data)) == 'innovaturelabs.com') {

            $user_name = (string) reset($email_data);

            if (!empty($user_name)) {

              // Check user already exists
              $get_user_by_username = $this->user_model->get_user_by_username($user_name, FALSE);

              if (empty($get_user_by_username['result'])) {
                $set_user = $this->user_model->set_user($user_name, md5($user_password), $inno_email);
                if ($set_user['status'] == 1 && !empty($set_user['result'])) {
                  $user_id = $set_user['result']['user_id'];
                  $act_token = md5($user_name . md5($user_password));
                  $act_link = site_url('signup/activate/' . $user_id . '/' . $act_token);
                  $log_msg = $user_name . ' (' . $inno_email . ') ' . 'from mobile ' . $this->input->ip_address();
                  system_log('signup', $log_msg);
                  $this->send_email($user_name, $user_password, $inno_email, $act_link);
                  $this->response['status'] = 1;
                  $this->response['message'] = 'Successfully signed up';
                  $this->response['data'] = array('user' => array('user_id' => $user_id), 'type' => 'signup');
                } else {
                  $this->response['message'] = 'Unable to create new user';
                }
              } elseif (!empty($get_user_by_username['result']) && ($get_user_by_username['result']['user_status'] == 0)) {
                $not_activated_user = $get_user_by_username['result'];
                $this->user_model->update_profile($not_activated_user['user_id'], array('user_password' => md5($user_password)));
                $act_token = md5($not_activated_user['user_name'] . md5($user_password));
                $act_link = site_url('signup/activate/' . $not_activated_user['user_id'] . '/' . $act_token);
                $log_msg = $not_activated_user['user_name'] . ' (' . $inno_email . ') ' . 'resend from ' . $this->input->ip_address();
                system_log('signup', $log_msg);
                $this->send_email($not_activated_user['user_name'], $user_password, $inno_email, $act_link);
                $this->response['status'] = 1;
                $this->response['message'] = 'Successfully resend sign up email';
                $this->response['data'] = array('user' => array('user_id' => $not_activated_user['user_id']), 'type' => 'resend');
              } else {
                $this->response['message'] = 'Email already exists';
              }
            } else {
              $this->response['message'] = 'Unable to generate username from email';
            }
          } else {
            $this->response['message'] = 'Innovature email required';
          }
        } else {
          $this->response['message'] = 'Email invalid';
        }
      } else {
        $this->response['message'] = 'Email required';
      }
    } else {
      $this->response['message'] = 'Invalid request';
    }
  }

  /**
   * Send email
   * 
   */
  private function send_email($user_name = NULL, $password = NULL, $user_email = NULL, $act_link = NULL) {
    $msg = "Username : {$user_name}<br/><br/>Password : {$password}<br/><br/>Activate : <a href='{$act_link}' target='_blank'>{$act_link}</>";
    $this->load->library('email');
    $this->email->clear(TRUE);
    $this->email->set_mailtype("html");
    $this->email->from('innotweets@gmail.com', 'innoTweets');
    //$this->email->to($user_email);
    $this->email->subject('innoTweets : SignUp');
    $this->email->message($msg);
    //$this->email->send();
    return;
  }

  /**
   * Print json output
   * 
   */
  public function __destruct() {
    echo json_encode($this->response);
  }

}

?>